package gz.copydatabase.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import gz.copydatabase.Application.ApplicationExd;
import gz.copydatabase.R;
import gz.copydatabase.Utils.DateUtils;
import gz.copydatabase.Utils.FileUtils;
import gz.copydatabase.Utils.SPUtils;
import gz.copydatabase.Utils.ShellUtils;


/**
 * Created by Administrator on 2016/11/24.
 */
public class ExportDialog extends Dialog {
    @Bind(R.id.progress_pb)
    ProgressBar progressPb;
    @Bind(R.id.percent_tv)
    TextView percentTv;
    @Bind(R.id.max_tv)
    TextView maxTv;
    @Bind(R.id.progress_tv)
    TextView progressTv;
    File inFile, outFile;
    private Listener listener;
    private int max;
    public ExportDialog(Context context) throws Exception {
        this(context, R.style.CustomDialog);
        String sdpath=Environment.getExternalStorageDirectory().toString();
        ShellUtils.execCommand("rm "+FileUtils.SDPATH+"tenement_passing_manager.db",true);
        ShellUtils.execCommand("cp /data/data/com.gz.gzcar/databases/tenement_passing_manager.db   "+sdpath ,true);
        inFile=new File(FileUtils.SDPATH+"tenement_passing_manager.db");
        max=(int)inFile.length();
        progressPb.setMax(max);
        maxTv.setText(""+ max);
        String file= DateUtils.date2String(new Date(),"yyyy年MM月dd日HH时mm分ss秒");
        outFile=new File(ApplicationExd.settingInfo.getString(SPUtils.Key.USB_PATH)+"/"+file+".db");
       // try {
        outFile.createNewFile();
        //}catch (Exception e){
           // Log.i("hello",e.toString());
       // }
       new ExportThread().start();
    }
    public void setListener(Listener listener) {
        this.listener = listener;
    }

    public ExportDialog(Context context, int theme) {
        super(context, theme);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        View view = LayoutInflater.from(context).inflate(R.layout.export_dialog, null);
        setCancelable(false);
        ButterKnife.bind(this, view);
        setContentView(view);
    }

    public interface Listener {
        void onListener();
    }
    @OnClick(R.id.x_iv)
    public void onClick(View view) {
        this.dismiss();
        ButterKnife.unbind(this);
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                int[] result = (int[]) msg.obj;
                progressPb.setProgress(result[0]);
                progressTv.setText("" + result[0]);
                percentTv.setText("" + result[1] + "%");
            } else {
                ExportDialog.this.dismiss();
                ButterKnife.unbind(this);
            }
        }
    };
    class ExportThread extends Thread {
        int[] result = {0, 0};
        public ExportThread() {
        }
        @Override
        public void run() {
            Message msg = null;
            try {
                FileInputStream fis = new FileInputStream(inFile); //读入原文件
                FileOutputStream fos = new FileOutputStream(outFile);
                byte[] buffer = new byte[500];
                int length;
                while ((length = fis.read(buffer)) != -1) {
                    result[0] += length; //字节数 文件大小
                    result[1] = result[0] * 100 / max;
                    fos.write(buffer, 0, length);
                    msg = Message.obtain();
                    msg.obj = result;
                    msg.what = 0;
                    handler.sendMessage(msg);
                }
                fis.close();
                fos.close();
                Thread.sleep(10000);
                msg = Message.obtain();
                msg.what = 1;
                handler.sendMessage(msg);
            } catch (Exception e) {
                Log.i("hello","hello"+e.toString());
            }
        }
    }
}